const express = require('express');
const router = express.Router();
const { getTopicData } = require('../model/game'); // 引入获取题目数据的方法
// GET home page.
router.get('/', function(req, res, next) {
    res.send({
        code: 200,
        msg: "ok"
    });
});
/* GET topic data by ID */
router.get('/GetTopicData', async (req, res) => {
    const topicID = req.query.TopicID; // 获取请求中的TopicID参数
    console.log('请求：',topicID)
    const token = req.headers.authorization.split(' ')[1]; // 去除 "Bearer " 后的 token
    console.log('收到的Token:',token);
    if (!token) {
      return res.status(401).send({
        code: 401,
        msg: '未授权访问，缺少 token'
      });
    }
    try {
        const topicData = await getTopicData(topicID); // 调用模型方法获取题目数据

        res.status(200).json({
            code: 200,
            msg: "Successfully retrieved topic data",
            data: topicData
        });
    } catch (error) {
        res.status(500).json({
            code: 500,
            msg: error.message
        });
    }
});

module.exports = router;